package com.aios.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aios.dao.SwitchPanelMapper;
import com.aios.dao.SystemUserMapper;
import com.aios.entity.SwitchPanel;
import com.aios.entity.SystemUser;
import com.aios.entity.unit.StatusEnum;
import com.aios.service.ExistValidationEnum;
import com.aios.service.SystemUserService;
import com.aios.util.GuidGenerator;
import com.aios.util.Pager;

@Service("systemUserService")
public class SystemUserServiceImpl extends AbstractListService implements SystemUserService {

	@Autowired 
	private SystemUserMapper systemUserMapper;
	
	@Autowired
	private SwitchPanelMapper switchPanelMapper;
	
	@Override
	public List<SystemUser> findAll() {
		return systemUserMapper.findAllByStatus(StatusEnum.ACTIVE.getStatusValue());
		//return null;
	}

	@Override
	public List<SystemUser> findAllNotDisabled() {
		return findAll();
	}

	@Override
	public List<SystemUser> findAllDisabled() {
		//return systemUserMapper.findAllByStatus(1);
		return null;
	}

	@Override
	public SystemUser findById(String id) {
		return systemUserMapper.selectByPrimaryKey(id);
		//return null;
	}

	@Override
	public int save(SystemUser entity) {
		return 0;
	}

	@Override
	public int update(SystemUser entity) {
		return this.systemUserMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int delete(SystemUser entity) {
		//return this.delete(entity.getId());
		return 0;
	}

	@Override
	public SystemUser signIn(String email, String password) {
		
		List<SystemUser> list = systemUserMapper.findByEmailAndPassword(email, password);
		
		if(list == null) {
			//error
		} else if(list.size() == 0) {
			//user not found
		} else if(list.size() > 1) {
			//too many users
		} else {
			return list.get(0);
		}
			
		return null;
	}
	
	public ExistValidationEnum emailExistValidation(String email) {
		List<SystemUser> list = systemUserMapper.findByEmailAddress(email);
		
		if(list == null || list.size() == 0) {
			return ExistValidationEnum.NOT_EXIST;
		} else if (list.size() == 1) {
			return ExistValidationEnum.EXIST;
		} else {
			return ExistValidationEnum.MULTIPLE_EXIST;
		}
	}

	@Override
	public int delete(String id) {
		 return systemUserMapper.deleteByPrimaryKey(id);
	}

	@Override
	public SystemUser selectActiveUserByPrimaryKey(String id) {
		return systemUserMapper.selectActiveUserByPrimaryKey(id, 0);
	}

	@Override
	public Pager viewPaginatedActiveList(Integer start, Integer size) {
		prepare(start, size);
		try {
			pager.setTotal(systemUserMapper.countAllByStatus(StatusEnum.ACTIVE.getStatusValue()));
			pager.setList(systemUserMapper.findListPaginated(StatusEnum.ACTIVE.getStatusValue(), start, size));
		}catch(Exception e) {
			//logger
			e.printStackTrace();
		}
		
		return pager;
	}

	@Override
	public int save(SystemUser entity, String userId) {
		entity.setId(GuidGenerator.systemUserKey());
		saveAddSettings(entity, userId);
		return systemUserMapper.insert(entity);
	}

	@Override
	public int update(SystemUser entity, String userId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int SystemUserDelete(String id) {
		List<SwitchPanel> list=systemUserMapper.findSwitchPanelBYSystemUserID(id);
		int val=0;
		
		if(list == null || list.size() == 0) {
			val = delete(id);
			return val;
		} else if (list.size() == 1) {
			val+= switchPanelMapper.deleteByPrimaryKey(list.get(0).getId());
			val+= delete(id);
			return val;
		} else {
			for(int i=0;i<list.size();i++){
				val = switchPanelMapper.deleteByPrimaryKey((list.get(0).getId()));
			}
			val = delete(id);
			return val;
		}
	}

	public String findByPannelId(String id) {
		// TODO Auto-generated method stub
		return null;
	}

}
